﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ExpenseSystem.Model;
using ExpenseSystem.Fether;
using ExpenseSystem.DataUpdater;
using ExpenseSystem.Common;
using ExpenseSystem.DataUpdater.Validation;

namespace ExpenseSystem.Core.ServiceOrderModules
{
    public class ServiceOrderUpdaterRegister
    {
        int rows;
        public NormalDataUpdater<ServiceOrderModel> GetServiceOrderUpdater()
        {
            NormalDataUpdater<ServiceOrderModel> systemDataUpdater = new NormalDataUpdater<ServiceOrderModel>();
            systemDataUpdater.DataUpdating += DataNotSpaceValid;
            systemDataUpdater.DataUpdating += SingletonValid;
            systemDataUpdater.DataUpdating += StateValid;
            systemDataUpdater.DataUpdating += OrderValid;
            systemDataUpdater.DataUpdating += EmployeeValid;
            systemDataUpdater.DataDeleting += CheckAvaliable;
            return systemDataUpdater;
        }
        private void CheckAvaliable(object sender, DataValidationEventArgs<ServiceOrderModel> e)
        {
            if (e.Delete.State!=Definition.STATE_NORMAL)
            {
                e.Cancle = true;
                e.Message = String.Format("[{0}]在非草稿状态下不能删除", e.Delete.Code);
            }

        }

        private void SingletonValid(object sender, DataValidationEventArgs<ServiceOrderModel> e)
        {
            ServiceOrderFetcher ServiceOrderFetcher = new ServiceOrderFetcher();
            string message = "";
            ServiceOrderModel ServiceOrder = ServiceOrderFetcher.GetByCode( e.Insert.Site, e.Insert.Code, out message);
            if (ServiceOrder != null && ServiceOrder.AutoId != e.Insert.AutoId)
            {
                e.Cancle = true;
                message = String.Format("代号[{0}]已被占用", ServiceOrder.Code);
                return;
            }
        }

        private void DataNotSpaceValid(object sender, DataValidationEventArgs<ServiceOrderModel> e)
        {
            if (String.IsNullOrWhiteSpace(e.Insert.Code))
            {
                e.Message = "代号不能为空";
                e.Cancle = true;
                return;
            }
        }

        private void StateValid(object sender,DataValidationEventArgs<ServiceOrderModel> e)
        {
            string message;
            if (e.Insert.State != Definition.STATE_NORMAL)
            {
                ServiceOrderDetailFetcher serviceOrderDetailFetcher = new ServiceOrderDetailFetcher();
                int detailCounter = serviceOrderDetailFetcher.ListByMainId(e.Insert.Site,e.Insert.AutoId,out message,out rows).Count;
                if(detailCounter==0)
                {
                    e.Message = String.Format("非草稿状态下必须具备明细信息[{0}]",e.Insert.Code);
                    e.Cancle = true;
                    return;
                }
                if (e.Insert.PropertyUpdated.Contains("Code") ||
                     e.Insert.PropertyUpdated.Contains("ServiceDate") ||
                     e.Insert.PropertyUpdated.Contains("Tel") ||
                     e.Insert.PropertyUpdated.Contains("Address") ||
                     e.Insert.PropertyUpdated.Contains("OrderId") ||
                     e.Insert.PropertyUpdated.Contains("CustomServiceId")
                    )
                {
                    e.Message = String.Format("单据内容不允许修改");
                    e.Cancle = true;
                    return;
                }
            }

            if(e.Insert.State==Definition.STATE_NORMAL)
            {
                if (e.Insert.AutoId > 0)
                {
                    ServiceBonusFetcher serviceBonusFetcher = new ServiceBonusFetcher();
                    int bonusCounter = serviceBonusFetcher.ListByServiceOrderId(e.Insert.Site, e.Insert.AutoId, out message, out rows).Count;
                    if (bonusCounter > 0)
                    {
                        e.Message = String.Format("非草稿状态下不能具备下游信息[{0}]", e.Insert.Code);
                        e.Cancle = true;
                        return;
                    }
                }
            }
        }
        private void EmployeeValid(object sender,DataValidationEventArgs<ServiceOrderModel> e)
        {
            EmployeeFetcher employeeFetcher = new EmployeeFetcher();
            string message;
            if(employeeFetcher.Get(e.Insert.Site,e.Insert.CustomServiceId,out message)==null)
            {
                e.Message = String.Format("客服人员信息[1]不正确[{0}]", e.Insert.Code, e.Insert.CustomServiceId);
                e.Cancle = true;
                return;
            }
        }
        private void OrderValid(object sender,DataValidationEventArgs<ServiceOrderModel> e)
        {
            OrderFetcher orderFetcher = new OrderFetcher();
            string message;
            OrderModel order = orderFetcher.Get(e.Insert.Site, e.Insert.OrderId, out message);
            if (order==null||order.State == Definition.STATE_NORMAL||order.State == Definition.STATE_READY)
            {
                e.Message = String.Format("来源订单号信息[{1}]不正确[{0}]", e.Insert.Code,e.Insert.OrderId);
                e.Cancle = true;
                return;
            }
        }
    }
}
